package com.bootdo.cms.controller;

import com.bootdo.cms.domain.ChannelDO;
import com.bootdo.cms.service.ChannelService;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 空白
 * @version 1.0
 * Create by 2019/11/14 01:16
 */
@Controller
@RequestMapping("/cms/video/channel")
public class ChannelController extends BaseController {
    String prefix = "cms/channel";

    @Autowired
    private ChannelService channelService;

    @GetMapping
    String channel(Model model) {
        return prefix+"/channel";
    }

    @RequestMapping("/list")
    @ResponseBody
    List<ChannelDO> list(@RequestParam Map<String, Object> params) {
        List<ChannelDO> channels = channelService.list(params);
        return channels;
    }

    @Log("添加视频栏目")
    @RequiresPermissions("cms:channel:add")
    @GetMapping("/add/{pId}")
    String add(Model model, @PathVariable Long pId) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", channelService.get(pId).getName());
        }
        return prefix + "/add";
    }

    @Log("编辑菜单")
    @RequiresPermissions("cms:channel:edit")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable Long id) {
        ChannelDO cdo = channelService.get(id);
        Long pId = cdo.getParentId();
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", channelService.get(pId).getName());
        }
        model.addAttribute("channel", cdo);
        return prefix+"/edit";
    }

    @Log("保存菜单")
    @RequiresPermissions("cms:channel:add")
    @PostMapping("/save")
    @ResponseBody
    R save(ChannelDO channel) {
        channel.setCreateTime(new Date());
        if (channelService.save(channel) > 0) {
            return R.ok();
        } else {
            return R.error(1, "保存失败");
        }
    }

    @Log("更新菜单")
    @RequiresPermissions("cms:channel:edit")
    @PostMapping("/update")
    @ResponseBody
    R update(ChannelDO channel) {
        channel.setUpdateTime(new Date());
        if (channelService.update(channel) > 0) {
            return R.ok();
        } else {
            return R.error(1, "更新失败");
        }
    }

    @Log("删除菜单")
    @RequiresPermissions("cms:channel:remove")
    @PostMapping("/remove")
    @ResponseBody
    R remove(Long id) {
        if (channelService.remove(id) > 0) {
            return R.ok();
        } else {
            return R.error(1, "删除失败");
        }
    }

    @GetMapping("/tree")
    @ResponseBody
    Tree<ChannelDO> tree() {
        Tree<ChannelDO> tree = channelService.getTree();
        return tree;
    }

    @GetMapping("/treeView")
    String treeView() {
        return prefix + "/channelTree";
    }
}
